﻿#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <stdlib.h>
#include <ctime>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

// 定义一个数组存储每个月的天数（平年情况，2月默认28天，后续根据闰年判断调整2月天数）
const int mon_table[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

// 判断是否为闰年的函数，返回1表示闰年，0表示平年
int is_leap_year(int year) {
    if (year % 4 == 0) {
        if (year % 100 == 0) {
            if (year % 400 == 0)
                return 1;
            else
                return 0;
        }
        else
            return 1;
    }
    else
        return 0;
}

// 根据时间戳（秒数）计算年月日时分秒以及星期的函数
void calculate_date_and_week_from_timestamp(time_t timestamp_seconds, int* year, int* month, int* day, int* hour, int* minute, int* second, int* weekday) {
    time_t temp_seconds = timestamp_seconds + 8 * 3600;
    int temp_year = 1970;

    // 计算年份
    while (temp_seconds >= (is_leap_year(temp_year) ? 31622400 : 31536000)) {
        temp_seconds -= (is_leap_year(temp_year) ? 31622400 : 31536000);
        temp_year++;
    }
    *year = temp_year;

    // 计算月份
    int temp_month = 0;
    while (temp_seconds > 0) {
        int days_in_month = mon_table[temp_month];
        if (is_leap_year(*year) && temp_month == 1) {
            days_in_month = 29;
        }
        if (temp_seconds >= days_in_month * 86400) {
            temp_seconds -= days_in_month * 86400;
            temp_month++;
        }
        else {
            break;
        }
    }
    *month = temp_month + 1;

    // 计算日期
    *day = temp_seconds / 86400 + 1;

    // 计算时分秒
    *hour = (temp_seconds % 86400) / 3600;
    *minute = (temp_seconds % 3600) / 60;
    *second = temp_seconds % 60;

    // 计算星期（以1970年1月1日为星期四，取值范围0 - 6，0代表星期四，依次类推）
    int total_days = 0;
    for (int y = 1970; y < *year; y++) {
        total_days += is_leap_year(y) ? 366 : 365;
    }
    for (int m = 0; m < *month - 1; m++) {
        total_days += mon_table[m];
        if (*year && m == 1) {
            total_days += is_leap_year(*year);
        }
    }
    total_days += *day - 1;
    *weekday = (total_days + 4) % 7;
}

#include <cassert>

int main() {
    //int timestamp_seconds = 1609302251;  // 示例时间戳，可替换为实际值

    time_t timestamp = 1735619051;
    int i = 0;
    for (;;) {



        //int timestamp_seconds = 1735647872;  // 示例时间戳，可替换为实际值
        int year, month, day, hour, minute, second, weekday;
        calculate_date_and_week_from_timestamp(timestamp, &year, &month, &day, &hour, &minute, &second, &weekday);
        printf("对应的日期为: %d年%d月%d日 %d时%d分%d秒 星期%d\n", year, month, day, hour, minute, second, weekday);

        //calculate_date_from_timestamp(timestamp, &year, &month, &day);
        //printf("对应的日期为: %d年%d月%d日\n", year, month, day);
        tm* timeinfo = localtime(&timestamp);


        assert(year == timeinfo->tm_year + 1900);
        assert(month == timeinfo->tm_mon + 1);
        assert(day == timeinfo->tm_mday);
        assert(hour == timeinfo->tm_hour);
        assert(minute == timeinfo->tm_min);
        assert(second == timeinfo->tm_sec);

        timestamp+=3600;
        if (i++ == 5000) {
            printf("对应的日期为: %d年%d月%d日\n", year, month, day);
            i = 0;
        }

    }
    return 0;
}